﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExaminationSys.DataAccess;
using ExaminationSys.DataAccess.Repository;
using Ext.Net;
using ExaminationSys.Helper;

namespace ExaminationSys.View.News
{
    public partial class HelperInfoEdit : System.Web.UI.UserControl
    {
        private static readonly NewsService Proxy = ObjectFactory<NewsService>.Create();

        private static readonly NewsTypeService ProxyNewsType = ObjectFactory<NewsTypeService>.Create();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!X.IsAjaxRequest)
            {
                Bind();
            }

            NewsContent.EnableSourceEdit = false;
            NewsContent.EnableFont = false;
            NewsContent.EnableColors = false;
            NewsContent.UpdateToolbar();
        }
        private void Bind()
        {
            var items = ProxyNewsType.GetPublishList();
            items = items.Where(c => c.Name == "帮助").ToList();
           // NewsTypeStore.DataSource = items;
           // NewsTypeStore.DataBind();

            if (items.Count > 0)
            {
                NewsTypeName.Text = "帮助";
                NewsTypeIDHidden.Value = items[0].ID.ToString();
                // NewsType.Select(1);
                // NewsType.UpdateLayout();
                //// NewsType.SelectedItem.Index = 0;
                // NewsType.ReadOnly = true;
                //var id = items.Where(c => c.Name == "信息快递").Select(c => c.ID).FirstOrDefault();
                //NewsType.Select(id);
            }
        }

        public Store GridStore { get; set; }
        public string ID { get; set; }


        public void SetData(string id, string title, string content, int status)
        {
            NewsTitle.Text = title;
            NewsStatus.Checked = status == 1;
            NewsContent.Text = content;



            //set tips for htmleditor
            //var tips=new HtmlEditorButtonTips();
            //tips.Bold.Title = "粗体 (Ctrl+B)";
            //tips.Bold.Text = "设置字体样式为粗体";

            //tips.Italic.Title = "斜体 (Ctrl+I)";
            //tips.Italic.Text = "设置字体样式为斜体";

            ID = id;
            X.Js.Call("setID", id);
        }

        public void Show()
        {
            this.ExpressInfoEditWindow.Show();
        }

        protected void SaveExpressInfo(object sender, DirectEventArgs e)
        {
            var title = NewsTitle.Text;
            var content = NewsContent.Text;
            var status = NewsStatus.Checked ? 1 : 0;
            var typeid = NewsTypeIDHidden.Value;

            if (string.IsNullOrWhiteSpace(title))
            {
                MessageBoxHelper.ShowWarning("标题不能为空");
                return;
            }
            if (string.IsNullOrWhiteSpace(content))
            {
                MessageBoxHelper.ShowWarning("内容不能为空");
                return;
            }
            if (string.IsNullOrEmpty(typeid))
            {
                MessageBoxHelper.ShowWarning("TypeID不能为空");
                return;
            }

            var id = ExpressIDHidden.Value;
            Guid realId = string.IsNullOrEmpty(id) ? Guid.NewGuid() : new Guid(id);
            if (Proxy.CheckTitleExists(title, realId))
            {
                MessageBoxHelper.ShowWarning(string.Format("标题 {0} 已经存在", title));
                return;

            }


            ExaminationSys.DataAccess.Entity.News entity = new DataAccess.Entity.News();
            entity.ID = realId;
            entity.Title = title;
            entity.Content = content;
            entity.IsRecommend = IsRecommend.Checked;
            entity.TypeID = new Guid(typeid);
            entity.Status = status;

            var op = FormsAuthenticationHelper.GetCurrentUserID();
            if (string.IsNullOrEmpty(op))
            {
                FormsAuthenticationHelper.SignOut();
                Response.Redirect("~/Account/Login.aspx");
            }

            entity.Operator = op;

            entity.OperatorTime = DateTime.Now; 
            var sucess = false;
           // entity.
            if (string.IsNullOrWhiteSpace(id))
            {
                var retCode = Proxy.Insert(entity);
                sucess = retCode != null;
            }
            else
            {
                sucess = Proxy.Update(entity);
            }

            if (sucess)
            {
                MessageBoxHelper.ShowInfo("保存成功");
                GridStore.Reload();
                ExpressInfoEditWindow.Hide();
            }
            else
            {
                MessageBoxHelper.ShowInfo("保存失败");
                return;

            }
        }
    }
}